//
//  PhotoBrowserViewController.swift
//  weibo10
//
//  Created by 晁岳山 on 15/10/27.
//  Copyright © 2015年 晁岳山. All rights reserved.
//

import UIKit

<<<<<<< HEAD
private let photoBrowserViewCellId = "photoBrowserViewCellId"
=======
>>>>>>> 31b3621e0c1aa1da74379c637e43582d7ba4ddfe
class PhotoBrowserViewController: UIViewController {
    
    var indexPath: NSIndexPath
    var urls: [NSURL]
    
    // MAEK: - 按钮点击事件
    @objc private func close() {
        print("关闭按钮")
    }
    @objc private func save() {
        print("保存按钮")
    }
    
    // MARK: - 视图生命周期
    override func loadView() {
<<<<<<< HEAD
        super.loadView()
        //如果重写了loadView的话 不写父类的loadView  一定要创建View 并且指定大小
//        view = UIView(frame: CGRectZero)
=======
        view = UIView()
>>>>>>> 31b3621e0c1aa1da74379c637e43582d7ba4ddfe
        setupUI()
    }
    override func viewDidLoad() {
        super.viewDidLoad()
<<<<<<< HEAD
//        setupUI()
=======
>>>>>>> 31b3621e0c1aa1da74379c637e43582d7ba4ddfe
    }
    // MARK: - 构造函数
    init(indexPath: NSIndexPath, urls: [NSURL]){
        self.indexPath = indexPath
        self.urls = urls
        super.init(nibName: nil, bundle: nil)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    // MAKR: - 懒加载
<<<<<<< HEAD
    private lazy var collectionView: UICollectionView = UICollectionView(frame: CGRectZero, collectionViewLayout: PhotoBrowserViewLayout())
=======
    private lazy var collectionView: UICollectionView = UICollectionView()
>>>>>>> 31b3621e0c1aa1da74379c637e43582d7ba4ddfe
    private lazy var closeButton: UIButton = UIButton(imageName: nil,
        fontSize: 16,
        btnName: "关闭",
        color: UIColor.blackColor(),
        backGroundColor: UIColor.whiteColor())
    private lazy var saveButton: UIButton = UIButton(imageName: nil,
        fontSize: 16,
        btnName: "保存",
        color: UIColor.blackColor(),
        backGroundColor: UIColor.whiteColor())
<<<<<<< HEAD
    
    private class PhotoBrowserViewLayout: UICollectionViewFlowLayout {
        private override func prepareLayout() {
            super.prepareLayout()
            itemSize = collectionView!.bounds.size
            minimumLineSpacing = 0
            minimumInteritemSpacing = 0
            scrollDirection = .Horizontal
            collectionView?.bounces = false
            collectionView?.pagingEnabled = true
            collectionView?.showsHorizontalScrollIndicator = false
        }
    }
=======
>>>>>>> 31b3621e0c1aa1da74379c637e43582d7ba4ddfe
}
// MARK: - 搭建界面
extension PhotoBrowserViewController {
    // MARK: - setupUI
    private func setupUI() {
        view.backgroundColor = UIColor.blackColor()
<<<<<<< HEAD
        collectionView.backgroundColor = UIColor.blueColor()
=======
>>>>>>> 31b3621e0c1aa1da74379c637e43582d7ba4ddfe
        // 1. 添加控件
        view.addSubview(collectionView)
        view.addSubview(closeButton)
        view.addSubview(saveButton)
        // 2.设置自动布局
        collectionView.frame = view.bounds
        closeButton.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(view.snp_left).offset(StatusCellPictureItemMargin)
<<<<<<< HEAD
            make.bottom.equalTo(view.snp_bottom).offset(-StatusCellPictureItemMargin)
            make.height.equalTo(36)
            make.width.equalTo(100)
            
        }
        saveButton.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(view.snp_right).offset(-StatusCellPictureItemMargin)
            make.bottom.equalTo(view.snp_bottom).offset(-StatusCellPictureItemMargin)
            make.height.equalTo(36)
            make.width.equalTo(100)
        }
         // 3.添加按钮事件
        saveButton.addTarget(self, action: "close", forControlEvents: .TouchUpInside)
        closeButton.addTarget(self, action: "save", forControlEvents: .TouchUpInside)
        //准备collectionView
        prepareCollectionView()
    }
    ///准备collectionView
    func prepareCollectionView() {
        //注册cell
        collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: photoBrowserViewCellId)
        collectionView.dataSource = self
    }
}
// MARK: - UICollectionViewDataSource - collectionView的数据源方法
extension PhotoBrowserViewController: UICollectionViewDataSource {
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 10
    }
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(photoBrowserViewCellId, forIndexPath: indexPath)
        cell.backgroundColor = UIColor.randomColor()
        return cell
    }
=======
            make.bottom.equalTo(view.snp_bottom).offset(StatusCellPictureItemMargin)
            make.height.equalTo(100)
            make.width.equalTo(36)
            
        }
        saveButton.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(view.snp_right).offset(StatusCellPictureItemMargin)
            make.bottom.equalTo(view.snp_bottom).offset(StatusCellPictureItemMargin)
            make.height.equalTo(100)
            make.width.equalTo(36)
        }
        // 3.添加按钮事件
        saveButton.addTarget(self, action: "", forControlEvents: .TouchUpInside)
        closeButton.addTarget(self, action: "", forControlEvents: .TouchUpInside)
    }
    
>>>>>>> 31b3621e0c1aa1da74379c637e43582d7ba4ddfe
}